home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / interp / perl5.005.tar.gz / perl5.005.tar / perl5.005 / t / op / local.t < prev    next >
Text File  |  1998-07-20  |  5KB  |  201 lines

  1. #!./perl
  2.  
  3. # $RCSfile: local.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:04 $
  4.  
  5. print "1..58\n";
  6.  
  7. # XXX known to leak scalars
  8. $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
  9.  
  10. sub foo {
  11.     local($a, $b) = @_;
  12.     local($c, $d);
  13.     $c = "ok 3\n";
  14.     $d = "ok 4\n";
  15.     { local($a,$c) = ("ok 9\n", "ok 10\n"); ($x, $y) = ($a, $c); }
  16.     print $a, $b;
  17.     $c . $d;
  18. }
  19.  
  20. $a = "ok 5\n";
  21. $b = "ok 6\n";
  22. $c = "ok 7\n";
  23. $d = "ok 8\n";
  24.  
  25. print &foo("ok 1\n","ok 2\n");
  26.  
  27. print $a,$b,$c,$d,$x,$y;
  28.  
  29. # same thing, only with arrays and associative arrays
  30.  
  31. sub foo2 {
  32.     local($a, @b) = @_;
  33.     local(@c, %d);
  34.     @c = "ok 13\n";
  35.     $d{''} = "ok 14\n";
  36.     { local($a,@c) = ("ok 19\n", "ok 20\n"); ($x, $y) = ($a, @c); }
  37.     print $a, @b;
  38.     $c[0] . $d{''};
  39. }
  40.  
  41. $a = "ok 15\n";
  42. @b = "ok 16\n";
  43. @c = "ok 17\n";
  44. $d{''} = "ok 18\n";
  45.  
  46. print &foo2("ok 11\n","ok 12\n");
  47.  
  48. print $a,@b,@c,%d,$x,$y;
  49.  
  50. eval 'local($$e)';
  51. print +($@ =~ /Can't localize through a reference/) ? "" : "not ", "ok 21\n";
  52.  
  53. eval 'local(@$e)';
  54. print +($@ =~ /Can't localize through a reference/) ? "" : "not ", "ok 22\n";
  55.  
  56. eval 'local(%$e)';
  57. print +($@ =~ /Can't localize through a reference/) ? "" : "not ", "ok 23\n";
  58.  
  59. # Array and hash elements
  60.  
  61. @a = ('a', 'b', 'c');
  62. {
  63.     local($a[1]) = 'foo';
  64.     local($a[2]) = $a[2];
  65.     print +($a[1] eq 'foo') ? "" : "not ", "ok 24\n";
  66.     print +($a[2] eq 'c') ? "" : "not ", "ok 25\n";
  67.     undef @a;
  68. }
  69. print +($a[1] eq 'b') ? "" : "not ", "ok 26\n";
  70. print +($a[2] eq 'c') ? "" : "not ", "ok 27\n";
  71. print +(!defined $a[0]) ? "" : "not ", "ok 28\n";
  72.  
  73. @a = ('a', 'b', 'c');
  74. {
  75.     local($a[1]) = "X";
  76.     shift @a;
  77. }
  78. print +($a[0].$a[1] eq "Xb") ? "" : "not ", "ok 29\n";
  79.  
  80. %h = ('a' => 1, 'b' => 2, 'c' => 3);
  81. {
  82.     local($h{'a'}) = 'foo';
  83.     local($h{'b'}) = $h{'b'};
  84.     print +($h{'a'} eq 'foo') ? "" : "not ", "ok 30\n";
  85.     print +($h{'b'} == 2) ? "" : "not ", "ok 31\n";
  86.     local($h{'c'});
  87.     delete $h{'c'};
  88. }
  89. print +($h{'a'} == 1) ? "" : "not ", "ok 32\n";
  90. print +($h{'b'} == 2) ? "" : "not ", "ok 33\n";
  91. print +($h{'c'} == 3) ? "" : "not ", "ok 34\n";
  92.  
  93. # check for scope leakage
  94. $a = 'outer';
  95. if (1) { local $a = 'inner' }
  96. print +($a eq 'outer') ? "" : "not ", "ok 35\n";
  97.  
  98. # see if localization works when scope unwinds
  99. local $m = 5;
  100. eval {
  101.     for $m (6) {
  102.     local $m = 7;
  103.     die "bye";
  104.     }
  105. };
  106. print $m == 5 ? "" : "not ", "ok 36\n";
  107.  
  108. # see if localization works on tied arrays
  109. {
  110.     package TA;
  111.     sub TIEARRAY { bless [], $_[0] }
  112.     sub STORE { print "# STORE [@_]\n"; $_[0]->[$_[1]] = $_[2] }
  113.     sub FETCH { my $v = $_[0]->[$_[1]]; print "# FETCH [@_=$v]\n"; $v }
  114.     sub CLEAR { print "# CLEAR [@_]\n"; @{$_[0]} = (); }
  115.     sub FETCHSIZE { scalar(@{$_[0]}) }
  116.     sub SHIFT { shift (@{$_[0]}) }
  117.     sub EXTEND {}
  118. }
  119.  
  120. tie @a, 'TA';
  121. @a = ('a', 'b', 'c');
  122. {
  123.     local($a[1]) = 'foo';
  124.     local($a[2]) = $a[2];
  125.     print +($a[1] eq 'foo') ? "" : "not ", "ok 37\n";
  126.     print +($a[2] eq 'c') ? "" : "not ", "ok 38\n";
  127.     @a = ();
  128. }
  129. print +($a[1] eq 'b') ? "" : "not ", "ok 39\n";
  130. print +($a[2] eq 'c') ? "" : "not ", "ok 40\n";
  131. print +(!defined $a[0]) ? "" : "not ", "ok 41\n";
  132.  
  133. {
  134.     package TH;
  135.     sub TIEHASH { bless {}, $_[0] }
  136.     sub STORE { print "# STORE [@_]\n"; $_[0]->{$_[1]} = $_[2] }
  137.     sub FETCH { my $v = $_[0]->{$_[1]}; print "# FETCH [@_=$v]\n"; $v }
  138.     sub DELETE { print "# DELETE [@_]\n"; delete $_[0]->{$_[1]}; }
  139.     sub CLEAR { print "# CLEAR [@_]\n"; %{$_[0]} = (); }
  140. }
  141.  
  142. # see if localization works on tied hashes
  143. tie %h, 'TH';
  144. %h = ('a' => 1, 'b' => 2, 'c' => 3);
  145.  
  146. {
  147.     local($h{'a'}) = 'foo';
  148.     local($h{'b'}) = $h{'b'};
  149.     print +($h{'a'} eq 'foo') ? "" : "not ", "ok 42\n";
  150.     print +($h{'b'} == 2) ? "" : "not ", "ok 43\n";
  151.     local($h{'c'});
  152.     delete $h{'c'};
  153. }
  154. print +($h{'a'} == 1) ? "" : "not ", "ok 44\n";
  155. print +($h{'b'} == 2) ? "" : "not ", "ok 45\n";
  156. print +($h{'c'} == 3) ? "" : "not ", "ok 46\n";
  157.  
  158. @a = ('a', 'b', 'c');
  159. {
  160.     local($a[1]) = "X";
  161.     shift @a;
  162. }
  163. print +($a[0].$a[1] eq "Xb") ? "" : "not ", "ok 47\n";
  164.  
  165. # now try the same for %SIG
  166.  
  167. $SIG{TERM} = 'foo';
  168. $SIG{INT} = \&foo;
  169. $SIG{__WARN__} = $SIG{INT};
  170. {
  171.     local($SIG{TERM}) = $SIG{TERM};
  172.     local($SIG{INT}) = $SIG{INT};
  173.     local($SIG{__WARN__}) = $SIG{__WARN__};
  174.     print +($SIG{TERM}        eq 'main::foo') ? "" : "not ", "ok 48\n";
  175.     print +($SIG{INT}        eq \&foo) ? "" : "not ", "ok 49\n";
  176.     print +($SIG{__WARN__}    eq \&foo) ? "" : "not ", "ok 50\n";
  177.     local($SIG{INT});
  178.     delete $SIG{__WARN__};
  179. }
  180. print +($SIG{TERM}    eq 'main::foo') ? "" : "not ", "ok 51\n";
  181. print +($SIG{INT}    eq \&foo) ? "" : "not ", "ok 52\n";
  182. print +($SIG{__WARN__}    eq \&foo) ? "" : "not ", "ok 53\n";
  183.  
  184. # and for %ENV
  185.  
  186. $ENV{_X_} = 'a';
  187. $ENV{_Y_} = 'b';
  188. $ENV{_Z_} = 'c';
  189. {
  190.     local($ENV{_X_}) = 'foo';
  191.     local($ENV{_Y_}) = $ENV{_Y_};
  192.     print +($ENV{_X_} eq 'foo') ? "" : "not ", "ok 54\n";
  193.     print +($ENV{_Y_} eq 'b') ? "" : "not ", "ok 55\n";
  194.     local($ENV{_Z_});
  195.     delete $ENV{_Z_};
  196. }
  197. print +($ENV{_X_} eq 'a') ? "" : "not ", "ok 56\n";
  198. print +($ENV{_Y_} eq 'b') ? "" : "not ", "ok 57\n";
  199. print +($ENV{_Z_} eq 'c') ? "" : "not ", "ok 58\n";
  200.  
  201.